package com.chaoyi.service.Impl;

import com.chaoyi.config.WSUploadConfig;
import com.chaoyi.dao.mysql.KoreanVideoMapper;
import com.chaoyi.doman.koreanvideo.*;
import com.chaoyi.model.KoreanVideo;
import com.chaoyi.model.KoreanVideoDetail;
import com.chaoyi.service.KoreanVideoService;
import com.chaoyi.util.PageUtil;
import com.chaoyi.util.UUIDUtil;
import com.chaoyi.util.qiniu.UploadFileQiNiu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Part;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuanqicheng
 * @create 2018-10-下午2:04
 */
@Service
public class KoreanVideoServiceImpl implements KoreanVideoService {

    @Autowired
    private KoreanVideoMapper koreanVideoMapper;

    @Override
    public String uploadClassifyImg2WSCDN(Part uploadFile) throws Exception {
        String exten = uploadFile.getSubmittedFileName().substring(uploadFile.getSubmittedFileName().lastIndexOf("."));
        String fileName = UUIDUtil.getUUID() + exten;

        return UploadFileQiNiu.uploadFile(uploadFile, WSUploadConfig.koreanVideoImgUrlPath, fileName);

    }

    @Override
    public String uploadClassifyVideo2WSCDN(Part uploadFile) throws IOException, Exception {
        String exten = uploadFile.getSubmittedFileName().substring(uploadFile.getSubmittedFileName().lastIndexOf("."));
        String fileName = UUIDUtil.getUUID() + exten;
        return UploadFileQiNiu.uploadFile(uploadFile, WSUploadConfig.koreanVideoUrlPath, fileName);
    }

    @Override
    public int addVideoSave(KoreanVideo entity) {
        return koreanVideoMapper.addVideoSave(entity);
    }

    @Override
    public int findCount(KoreanVideo koreanVideo) {
        return koreanVideoMapper.findCount(koreanVideo);
    }

    @Override
    public List<KoreanVideo> findList(int offset, int limit,KoreanVideo koreanVideo) {
        return koreanVideoMapper.findList(offset, limit,koreanVideo);
    }

    @Override
    public KoreanVideo findVideoOne(int id) {
        return koreanVideoMapper.findVideoOne(id);
    }

    @Override
    public int edit(KoreanVideo entity) {

        return koreanVideoMapper.edit(entity);
    }

    @Override
    public int deleteVideo(int id) {
        return 0;
    }

    @Override
    public List<KoreanVideoOutput> getVideoClassificationList(KoreanVideoClassificationQueryCondition queryCondition) {

        int page = queryCondition.getPage();

        int pageSize = queryCondition.getPageSize();

        return koreanVideoMapper.getVideoClassificationList(PageUtil.getOffset(page, pageSize),pageSize,queryCondition);
    }

    @Override
    public List<KoreanOnlineVideoOutput> getKoreanOnlineVideoList(KoreanVideoQueryCondition queryCondition) {
        int page = queryCondition.getPage();

        int pageSize = queryCondition.getPageSize();

        return koreanVideoMapper.getKoreanOnlineVideoList(PageUtil.getOffset(page,pageSize),pageSize);
    }

    @Override
    public Map<String,String> getSee(String id) {

        String url = koreanVideoMapper.getSee(id);
        if(url == null){
            url = "";
        }
        Map<String,String> resultMap = new HashMap<>(0);
        resultMap.put("see_url",url);
        return resultMap ;
    }

    @Override
    public Map<String,String> getTryToSee(String id) {
        String url = koreanVideoMapper.getTryToSee(id);
        if(url==null){
            url = "";
        }

        Map<String,String> resultMap = new HashMap<>(0);
        resultMap.put("try_see_url",url);

        return resultMap ;
    }

    @Override
    public KoreanVideoDetail selectKoreanVideoDetailById(int id) {
        return koreanVideoMapper.selectKoreanVideoDetailById(id);
    }

    @Override
    public int saveDetail(KoreanVideoDetail entity) {
        return koreanVideoMapper.saveDetail(entity);
    }

    @Override
    public int editDetail(KoreanVideoDetail entity) {
        return koreanVideoMapper.editDetail(entity);
    }

    @Override
    public KoreanVideoDetailOutput getDetail(String id) {
        KoreanVideoDetailOutput koreanVideoDetailOutput =koreanVideoMapper.getDetail(id);
        if(koreanVideoDetailOutput!=null){
            String header = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body>";
            String end = "</body></html>";
            String content = koreanVideoDetailOutput.getDetailInfo();
            koreanVideoDetailOutput.setDetailInfo(header+content+end);
        }
        return koreanVideoDetailOutput;
    }
}
